package com.itheima;


import org.junit.Test;

import java.io.InputStream;
import java.util.logging.*;

public class JULTest {


    @Test
    public void testQuick() {


        //.创建JUL Logger对象无法传入class对象，只能传如："cn.xw.testDemo"
        Logger logger = Logger.getLogger(JULTest.class.getName());
        //.输出日志的两种方式，其中日志级别共有7种，还有2种特殊级别
        logger.severe("[SEVERE] 表明程序严重的失败或错误");
        logger.warning("[WARNING] 表明潜在的问题");
        // 带有占位符的日志打印
        String name = "蚂蚁小哥", age = "18";
        logger.log(Level.INFO, "姓名：{0},{1}", new Object[]{name, age});

    }

    @Test
    public void testLogConfig() throws Exception {
        //获取日志记录器对象
        Logger logger = Logger.getLogger("com.itheima.JULTest");
        //关闭系统默认配置
        logger.setUseParentHandlers(false);
        //自定义配置日志级别
        //创建ConsoleHandler
        ConsoleHandler consoleHandler = new ConsoleHandler();
        //创建简单格式转换对象
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        //进行关联
        consoleHandler.setFormatter(simpleFormatter);
        logger.addHandler(consoleHandler);
        //配置日志具体级别
        logger.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);

        //场景FileHandler 文件输出
        FileHandler fileHandler = new FileHandler("./jul.log");
        //进行关联
        fileHandler.setFormatter(simpleFormatter);
        //设置日志文件的输出级别为CONFIG
        fileHandler.setLevel(Level.CONFIG);
        logger.addHandler(fileHandler);

        //日志记录输出
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.finer("fine");
        logger.finer("finer");
        logger.finest("finest");
    }

    @Test
    public void testLogParent() throws Exception {
        Logger logger1 = Logger.getLogger("com.itheima");
        Logger logger2 = Logger.getLogger("com");
        System.out.println(logger1.getParent() == logger2);
        //所有日志的顶级父类LogManager$RootLogger,name""
        String name = logger2.getParent().getName();
        System.out.println(logger2.getParent() + name);

        //关闭默认配置
        logger2.setUseParentHandlers(false);

        ConsoleHandler consoleHandler = new ConsoleHandler();
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        consoleHandler.setFormatter(simpleFormatter);
        logger2.addHandler(consoleHandler);

        logger2.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);

        logger1.severe("severe");
        logger1.warning("warning");
        logger1.info("info");
        logger1.config("config");
        logger1.fine("fine");
        logger1.finer("finer");
        logger1.finest("finest");
    }

    @Test
    public void testLogProperties() throws Exception {
        //读取配置文件，通过类加载器
        InputStream ins = JULTest.class.getClassLoader().getResourceAsStream("logging.properties");
        //创建LogManager
        LogManager logManager = LogManager.getLogManager();
        //通过LogManager加载配置文件
        logManager.readConfiguration(ins);
        //创建日志记录器

        Logger logger = Logger.getLogger("com.itheima");
        logger.severe("severe");
        logger.warning("warning");
        logger.info("info");
        logger.config("config");
        logger.fine("fine");
        logger.finer("finer");
        logger.finest("finest");

        Logger logger2 = Logger.getLogger("test");
        logger2.severe("severe test");
        logger2.warning("warning test");
        logger2.info("info test");
        logger2.config("config test");
        logger2.fine("fine test");
        logger2.finer("finer test");
        logger2.finest("finest test");
    }
}
